﻿using FreeSqlDemo.Modules;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FreeSqlDemo.Services
{
    public class User
    {

        public void GetUser()
        {
            var connectionString = "Data Source = 39.105.20.156; Port = 3306; User ID = root; Password = root; Initial Catalog = demo;";

            var connString = "server=39.105.20.156;Database=demo;Uid=root;Pwd=root;";
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "Server=39.105.20.156;Database=demo;Uid=root;Pwd=root;AllowLoadLocalInfile=true",
                DbType = DbType.MySql,
                IsAutoCloseConnection = true
            });

            var Fsql = new FreeSql.FreeSqlBuilder()
                         .UseConnectionString(FreeSql.DataType.MySql, connectionString)
                          .UseAutoSyncStructure(true)
                          .UseLazyLoading(true)
                          .UseNoneCommandParameter(true)
                          .Build();
            var items = new List<Topic>();
            for (var a = 2; a < 12; a++)
                items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100, CreateTime = Convert.ToDateTime("2022-01-10 16:19:52.321") });


            var list = db.Queryable<Topic>().ToList();

            Fsql.InsertOrUpdate<Topic>()
            .SetSource(items) //需要操作的数据
            .IfExistsDoNothing() //如果数据存在，啥事也不干（相当于只有不存在数据时才插入）
            .ExecuteAffrows();
            //var t1 = Fsql.Insert(items.First()).ExecuteAffrows();
            //var t2 = Fsql.Insert(items).ExecuteAffrows();
        }
    }
}
